<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Model\Refund;
use Illuminate\Http\Request;

/**
 * 退款管理
 */
class RefundController extends Controller
{
    /**
     * 定义模型
     */
    protected $model;

    /**
     * 定义验证
     */
    protected $validate;

    /**
     * 初始化控制器
     */
    public function __construct()
    {
        $this->model = (new Refund());
        $this->validate = (new \App\Validate\Admin\Refund());
    }

    /**
     * 退款映射
     */
    public function refund_mapping()
    {
        // 退款类型
        $refund_type = $this->model->getRefundTypeList();
        // 状态
        $status = $this->model->getStatusList();
        $this->success(['refund_type' => $refund_type, 'status' => $status]);
    }

    /**
     * 退款列表
     */
    public function refund_list(Request $request)
    {
        // 获取参数
        $params = $request->all();
        // 设置对象
        $model = $this->model;
        // id
        if (isset($params['refund_id']) && $params['refund_id']) {
            $model = $model->where('id', $params['refund_id']);
        }
        // 退款单号
        if (isset($params['refund_sn']) && $params['refund_sn']) {
            $model = $model->where('refund_sn', 'like', '%' . $params['refund_sn'] . '%');
        }
        // 退款类型
        if (isset($params['refund_type']) && $params['refund_type']) {
            $model = $model->where('refund_type', $params['refund_type']);
        }
        // 商品编码
        if (isset($params['goods_sn']) && $params['goods_sn']) {
            $model = $model->where('goods_sn', 'like', '%' . $params['goods_sn'] . '%');
        }
        // 规格编码
        if (isset($params['sku_sn']) && $params['sku_sn']) {
            $model = $model->where('sku_sn', 'like', '%' . $params['sku_sn'] . '%');
        }
        // 条形码
        if (isset($params['bar_code']) && $params['bar_code']) {
            $model = $model->where('bar_code', 'like', '%' . $params['bar_code'] . '%');
        }
        // 商品标题
        if (isset($params['goods_title']) && $params['goods_title']) {
            $model = $model->where('goods_title', 'like', '%' . $params['goods_title'] . '%');
        }
        // 状态
        if (isset($params['status']) && $params['status']) {
            $model = $model->where('status', $params['status']);
        }
        // 创建时间
        if (!empty($params['start_createtime']) && !empty($params['end_createtime'])) {
            $model = $model->whereBetween('createtime', [$params['start_createtime'], $params['end_createtime']]);
        }
        // 修改时间
        if (!empty($params['start_updatetime']) && !empty($params['end_updatetime'])) {
            $model = $model->whereBetween('updatetime', [$params['start_updatetime'], $params['end_updatetime']]);
        }
        // 获取条数
        $pageSize = isset($params['pageSize']) && $params['pageSize'] ? $params['pageSize'] : 10;
        $total = $model
            ->with(['order'])
            ->whereHas('order', function ($query) use ($params) {
                // 订单编号
                if (isset($params['order_sn']) && $params['order_sn']) {
                    $query->where('order_sn', 'like', '%' . $params['order_sn'] . '%');
                }
            })
            ->count();
        $list = $model
            ->with('order')
            ->whereHas('order', function ($query) use ($params) {
                // 订单编号
                if (isset($params['order_sn']) && $params['order_sn']) {
                    $query->where('order_sn', 'like', '%' . $params['order_sn'] . '%');
                }
            })
            ->orderBy('id', 'desc')
            ->take($pageSize)
            ->get(['id as refund_id', 'order_id', 'refund_sn', 'refund_type','goods_sn', 'sku_sn', 'bar_code', 'goods_title',
                'goods_image', 'spec_value', 'goods_price', 'goods_num', 'refund_price', 'status',
                'createtime', 'updatetime'])
            ->toArray();
        foreach ($list as &$val) {
            $val['goods_image'] = replace_imgs_url($val['goods_image']);
        }
        $this->success(['total_count' => $total, 'total_page' => ceil($total / $pageSize), 'list' => $list]);
    }

    /**
     * 退款详情
     */
    public function refund_details(Request $request)
    {
        // 获取参数
        $ids = $request->input('refund_id');
        $row = $this->model->with('order')->where('id', $ids)->first();
        if (!$row) {
            $this->error('记录未找到');
        }
        $row['goods_image'] = replace_imgs_url($row['goods_image']);
        $row['logisticsInfo'] = [];
        $this->success($row);
    }
}
